package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;

import com.jwdf.uda.event.ThirdPartEvent;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.user.personal.QQVisitorUpdateInfoFragment;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.UdaUtils;

/**
 * Created by Administrator on 2016/7/7.
 */
public class QQVisitorUpdateInfoActivity extends AbsTitleBarActivity {

    public static void start(Context context, String nickName,String avatar, boolean isTop) {
        Intent intent = new Intent(context, QQVisitorUpdateInfoActivity.class);
        intent.putExtra("is_top", isTop);
        intent.putExtra("nick_name", nickName);
        intent.putExtra("avatar",avatar);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        return "完善个人信息";
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new QQVisitorUpdateInfoFragment();
    }

    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putString("is_top", getIntent().getBooleanExtra("is_top", false) ? "0" : "1");
        args.putString("nick_name", getIntent().getStringExtra("nick_name"));
        args.putString("avatar",getIntent().getStringExtra("avatar"));
        return args;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
    }

    @Override
    protected void onDestroy() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroy();
    }

    public void onEvent(ThirdPartEvent event) {
        if (ThirdPartEvent.EVENT_COMPLETE_INFO == event.getEvent()) {
            finish();
        }
    }


    @Override
    public void onHeadBackClick(View view) {
        UdaUtils.hideInputMethod(this, view);
        FragmentManager manager = getSupportFragmentManager();
        int count = manager.getBackStackEntryCount();
        if (count == 0) {
            doReset();
        } else {
            try {
                manager.popBackStackImmediate();
            } catch (IllegalStateException ignored) {
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            FragmentManager manager = getSupportFragmentManager();
            int count = manager.getBackStackEntryCount();
            if (count == 0) {
                doReset();
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    private void doReset(){
        ConfigUtils.removeCurrentUser(this);
        ConfigUtils.setHttpHeadersCookie(this,"");
        finish();
    }

}
